<template>
  <div class="search">
    <div class="left" @click="$router.push('/home')">
      <img src="../assets/oi.png" alt="">
    </div>
    <div class="inp">
      <div>
        <input type="text" placeholder=" 请输入您想要商品" v-model="word" @keydown.enter="getsearch">
        <el-button type="danger" icon="el-icon-search" @click="getsearch">搜索</el-button>
        <div class="seachbottom">
          <a>手机</a>
          <a>连衣裙</a>
          <a>智能家居</a>
          <p>{{userinfo}}</p>
        </div>
      </div>
    </div>
    <div class="search-right">
      <a @click="gotocart"><i class="el-icon-shopping-cart-2"></i>我的购物车</a>

    </div>

  </div>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  name: 'headerseach',
  data() {
    return {
      word: ''
    }
  },
  methods: {
    // 模糊搜索
    async getsearch() {
      if (this.word.trim()) {
        let keyword = this.word
        console.log(keyword)
        this.$store.dispatch('goods/getsearch', { keyword })
        this.$router.replace('/searchdetails')
      } else {
        this.$message({
          message: '空白内容搜索不到哦~',
          type: 'warning',
          duration: 1000
        })
      }
    },
    //购物车
    gotocart() {
      if (this.userinfo.id) {
        this.$router.replace('/shopcart')
      } else {
        this.$message({
          title: '提示',
          message: '请先登录'
        })
      }
    }
  },
  watch: {
    $route(to) {
      if (to.path == '/home') {
        this.word = ''
      }
    }
  },
  computed: {
    ...mapGetters(['userinfo'])
  }
}
</script>

<style scoped lang='less'>
.search {
  display: flex;
  background: #f7f7f7;
  .left {
    display: flex;
    height: 200px;
    width: 20%;

    justify-content: center;
    align-items: center;
    img {
      width: 150px;
      height: 150px;
      border-radius: 50%;
      border: 1px solid black;
      &:hover {
        transform-style: preserve-3d;
        transform: rotateY(360deg);
        transition: all 1500ms;
        -moz-transform-style: preserve-3d;
        -moz-transform: rotateY(45deg);
        -moz-transition: all 8000ms;
      }
    }
  }
  .inp {
    width: 60%;

    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;

    input {
      height: 35px;
      width: 350px;
      border: 1px solid #f56c6c;
      vertical-align: middle;
      outline: medium;
    }
    .el-button--danger {
      height: 38px;
      border-radius: 0;
    }
    .seachbottom {
      height: 20px;

      font-size: 12px;
      a {
        height: 20px;
        margin-right: 10px;
        &:hover {
          color: red;
        }
      }
    }
  }
  .search-right {
    width: 20%;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 14px;
    a {
      height: 20px;
      border: 1px solid #f56c6c;
      background: #f7f7f7;
      color: #f56c6c;
      i {
        margin-right: 5px;
      }
    }
  }
}
</style>